package com.googlecode.gwtbb.client.only.widget.basic;

import com.google.gwt.user.client.ui.*;
import com.googlecode.gwtbb.client.only.glue.TokenGenerator;

/**
 * @author Abraham Grief
 * @version $Id$
 * @since Jul 30, 2008
 */
public class HistoryTabPanel extends Composite implements TabListener, SourcesTabEvents, IndexedPanel {
	private HistoryTabBar tabBar;
	private DeckPanel deck;
	private TabListenerCollection tabListeners;

	public HistoryTabPanel() {
		VerticalPanel panel = new VerticalPanel();
		initWidget(panel);
		panel.add(tabBar = new HistoryTabBar());
		tabBar.addTabListener(this);
		panel.add(deck = new DeckPanel());
		panel.setCellHeight(deck, "100%");
		tabBar.setWidth("100%");
		setStyleName("gwtbb-HistoryTabPanel");
	}

	public void add(Widget widget, Widget tabWidget, TokenGenerator tokenGenerator) {
		insert(widget, tabWidget, deck.getWidgetCount(), tokenGenerator);
	}

	public void addTabListener(TabListener listener) {
		if (tabListeners == null) {
			tabListeners = new TabListenerCollection();
		}
		tabListeners.add(listener);
	}

	public Widget getWidget(int index) {
		return deck.getWidget(index);
	}

	public int getWidgetCount() {
		return deck.getWidgetCount();
	}

	public int getWidgetIndex(Widget child) {
		return deck.getWidgetIndex(child);
	}

	public boolean onBeforeTabSelected(SourcesTabEvents sender, int tabIndex) {
		return tabListeners == null || tabListeners.fireBeforeTabSelected(this, tabIndex);
	}

	public void onTabSelected(SourcesTabEvents sender, int tabIndex) {
		deck.showWidget(tabIndex);
		if (tabListeners != null) {
			tabListeners.fireTabSelected(this, tabIndex);
		}
	}

	public boolean remove(int index) {
		tabBar.removeTab(index);
		return deck.remove(index);
	}

	public void removeTabListener(TabListener listener) {
		if (tabListeners != null) {
			tabListeners.remove(listener);
		}
	}

	public void selectTab(int index) {
		tabBar.selectTab(index);
		deck.showWidget(index);
	}

	protected void insert(Widget w, Widget tabWidget, int beforeIndex, TokenGenerator tokenGenerator) {
		tabBar.insertTab(tabWidget, beforeIndex, tokenGenerator);
		deck.insert(w, beforeIndex);
	}
}
